﻿using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
using Xarmarin.GoldFinger.Engine.Core;
using Xarmarin.GoldFinger.Interface;

namespace Xarmarin.GoldFinger.Engine.Time
{
    public class TimeEngine
    {
        private long CurrtCount = 0;
        /// <summary>
        /// 初始化Timer控件
        /// </summary>
        public void InitTimer(int interval = 1000)
        {
            System.Timers.Timer timer;
            //获取上次离线时间

            //计算两次离线收益

            //END

            //时间间隔
            timer = new System.Timers.Timer(interval)
            {
                //设置执行一次（false）还是一直执行(true)
                AutoReset = true,
                //设置是否执行System.Timers.Timer.Elapsed事件
                Enabled = true,
            };
            //绑定Elapsed事件
            timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);
            timer.Start();
        }
        /// <summary>
        /// Timer类定时到点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                CurrtCount += 1;
                //修为增进
                ManaUp(Rand.RandCore.ManaRandValue);
                //千秒一年
                if (CurrtCount % 1000 == 0)
                {
                    YearsUp();
                }
            }
            catch (Exception ex)
            {
                DependencyService.Get<IToast>().ShortAlert("Timer Error:" + ex.Message);
            }
        }
        /// <summary>
        /// 更新修炼时间
        /// </summary>
        public void YearsUp()
        {
            //每年特殊事件触发

            //END
            KzContext.CurrSubject.Age++;
            //生命耗尽
            if (KzContext.CurrSubject.Age > KzContext.CurrSubject.LifeYear)
            {
                KzContext.CurrSubject.Status = 2;
                DependencyService.Get<IToast>().ShortAlert("寿元耗尽，无法窥得天机，寻得长生！");
            }
        }

        public System.Timers.Timer InitShortTimer(int interval = 1000)
        {
            var timer = new System.Timers.Timer(interval)
            {
                //设置执行一次（false）还是一直执行(true)
                AutoReset = true,
                //设置是否执行System.Timers.Timer.Elapsed事件
                Enabled = true,
            };
            //绑定Elapsed事件
            timer.Elapsed += new System.Timers.ElapsedEventHandler(ShortTimerUp);
            return timer;
        }
        private void ShortTimerUp(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                ManaUp(Rand.RandCore.ManaRandValue);
            }
            catch (Exception ex)
            {
                DependencyService.Get<IToast>().ShortAlert("Timer Error:" + ex.Message);
            }
        }
        public void ManaUp(long upValue)
        {
            KzContext.CurrSubject.Mana += upValue;
        }
    }
}
